FightViewFunc={}
FightView={}
FightViewFunc.__index=FightViewFunc
setmetatable(FightView, FightViewFunc)

FightState = {AttackBegin=1, Attacking=2, Hurt=3, AttackEnd=4}
FightView.attackerPos = 
                      {{75, 475},{175, 475},{275, 475}} 
                      {{75, 325},{175, 325},{275, 325}} 
                      {{75, 175},{175, 175},{275, 175}} 
FightView.defenderPos = 
                      {{75, 475},{175, 475},{275, 475}} 
                      {{75, 325},{175, 325},{275, 325}}
                      {{75, 175},{175, 175},{275, 175}}
--FightView.State = FightState. 

FightViewFunc.create = function(self, scene, attacks, defends)
  self.attackers = {}
  self.defenders = {}
  for k, v in pairs(attacks) do
    local spname = "hero1"
    local hv = HeroView:create(scene, spname, ccp(FightView.attackerPos[k][1], FightView.attackerPos[k][2))
    self.attackers[k] = hv 
  end
  for k, v in pairs(defends) do
    local spname = "hero1"
    local hv = HeroView:create(scene, spname, ccp(FightView.attackerPos[k][1], FightView.attackerPos[k][2))
    self.defenders[k] = hv 
  end
end

FightViewFunc.clear = function()
  self.attackers = nil
  self.currentAttacker = nil
  self.defenders = nil
end

FightViewFunc.stateChange = function(self, node)
  if self.State = FightState.AttackBegin then
    --播放攻击动作
    local action = ActionUtil.createAction(string.format("%s.plist", self.currentAttacker.name), 0.5)
    local ac = ActionUtil.createSeqAction({action,CCCallFuncN:create(onAction)})
    self.currentAttacker.normalView:runAction(ac)
    self.State = FightState.Attacking
  elseif self.State = FightState.Attacking then
    --子弹飞行
    CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile("skilleffect8.plist")
    for k,v in pairs(self.currentDefenders) do
      local ball = CCSprite:createWithSpriteFrameName("skilleffect8_1.png")
      ball:setPosition(av.normalView:getPosition())
      ball:setScale(0.5)
      av.normalView:getParent():addChild(ball)
      if FightView.ball==nil then 
        ball:runAction(ActionUtil.createSeqAction({CCMoveBy:create(0.5, ccp(v.normalView:getPosition())), CCCallFuncN:create(onAction)}))
        FightView.ball=ball 
      else
        ball:runAction(ActionUtil.createSeqAction({CCMoveBy:create(0.5, ccp(v.normalView:getPosition())), CCCallFuncN:create(removeFromParent)}))
      end
    end
    local effectPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("soundKnife.mp3")
    effectID = SimpleAudioEngine:sharedEngine():playEffect(effectPath) 
    self.State = FightState.Hurt  
  elseif self.State = FightState.Hurt then
    --受伤效果
    node:getParent():removeChild(node, true)
    for k,v in pairs(FightView.currentDefenders) do
      local ac = ActionUtil.createSeqAction({CCRotateBy:create(0.1, 15), CCRotateBy:create(0.1, -15), CCCallFuncN:create(onAction)})
      v.normalView:runAction(ac)
    end
    self.State = FightState.AttackEnd
  end
end

FightViewFunc.playAttack = function(self, aIndex, dIndexs, skill, buff, hurt)
  self.currentAttacker = self.attackers[aIndex]
  self.currentDefenders = {}
  for k,v in pairs(dIndexs) do
    self.currentDefenders[k] = self.defenders[v]
  end
  
  self.State = FightState.AttackBegin
  self.stateChange(nil) 
end


function removeFromParent(node)
  node:getParent():removeChild(node, true)
end

function onAction(node)
  FightView:stateChange(node)  
end
